-- MonsterBuffEffectM
-- Created by zouyb Jun/25/2015
-- 怪物BUFF光效

BUFF_TYPE_ATTACK   = 1;
BUFF_TYPE_HP       = 2;
DEBUFF_TYPE_ATTACK = 3;
DEBUFF_TYPE_HP     = 4;

MonsterBuffEffectM = class("MonsterBuffEffectM", function()
    return cc.Node:create();
end);

function MonsterBuffEffectM.create()
    return MonsterBuffEffectM.new();
end

-- 构造函数
function MonsterBuffEffectM:ctor()
	self:init();
end

-- 初始化
function MonsterBuffEffectM:init()
	-- 加载怪物的光效行为
    self.rules = LOAD_PATH("game/ui/buff/rules");

	-- 注册怪物出现时的回调
    EventMgr.register("MonsterBuffEffectM", event.UI_GRID_OPEN, function(params)
		local pos = params.pos;
		local grid = DungeonM.getGridByPos(pos);
        if grid.monster then
            performWithDelay(self, function() self:onMonsterAppear(grid.monster); end, 0.01);
		end
    end);

    EventMgr.register("MonsterBuffEffectM", event.GRID_REFRESH_MONSTER, function(params)
        local pos = params.pos;
        local grid = DungeonM.getGridByPos(pos);
        if grid.monster then
            -- UIDungeonMain中格子刷怪有延迟，此处需在此延迟上再加0.1秒
            performWithDelay(self, function() self:onMonsterAppear(grid.monster); end, GRID_REFRESH_MONSTER_DELAY + 0.1);
        end
    end);

	-- 注册怪物死亡时的回调
    EventMgr.register("MonsterBuffEffectM", event.MONSTER_DIE_LOGIC, function(params)
		local pos = params.pos;
		local grid = DungeonM.getGridByPos(pos);
		if grid.monster then
            performWithDelay(self, function() self:onMonsterDie(grid.monster); end, 0.01);
		end
    end);

	-- 注册怪物受到伤害时的回调
    EventMgr.register("MonsterBuffEffectM", event.MONSTER_DAMAGED, function(params)
		local pos = params.pos;
		local grid = DungeonM.getGridByPos(pos);
        if grid.monster then
            performWithDelay(self, function() self:onMonsterDamaged(grid.monster); end, 0.01);
		end
    end);

    -- 注册怪物变身时的回调
    EventMgr.register("MonsterBuffEffectM", event.MONSTER_TRANSFORM, function(params)
        local pos = params.pos;
        local grid = DungeonM.getGridByPos(pos);
        if grid.monster then
            performWithDelay(self, function() self:onMonsterTransform(grid.monster); end, GRID_REFRESH_MONSTER_DELAY + 0.1);
        end
    end);

    -- 注册钢铁之门被摧毁时的回调
    EventMgr.register("MonsterBuffEffectM", event.UI_STEEL_DOOR_ADD_DEBUF, function(params)
        performWithDelay(self, function() self:onSteelDoorDestory(); end, GRID_REFRESH_MONSTER_DELAY + 0.1);
    end);

    -- 检查下是不是有怪物已经直接打开了
    performWithDelay(self, function() self:checkMonsterAlreadyOpen(); end, 0.01);

    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            EventMgr.removeAll("MonsterBuffEffectM");

            if self.eventModuleNames ~= nil then
                for _, name in pairs(self.eventModuleNames) do
                    EventMgr.removeAll(name);
                end
            end
        end
    end);
end

-- 检查下是不是有怪物已经直接打开了
function MonsterBuffEffectM:checkMonsterAlreadyOpen()
    local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
    for i = 1, #openMonsterGrids do
        local monsterGrid = openMonsterGrids[i];
        if monsterGrid.gridData.monster then
            self:onMonsterAppear(monsterGrid.gridData.monster);
        end
    end
end

-- 怪物出现时回调
function MonsterBuffEffectM:onMonsterAppear(monster)
    local baseClassId = monster:query("base_class_id");

    local rule = self.rules[tostring(baseClassId)];
    if rule and rule.onAppear then
        rule.onAppear(monster);
    end
end

-- 怪物死亡时回调
function MonsterBuffEffectM:onMonsterDie(monster)
    local baseClassId = monster:query("base_class_id");

    local rule = self.rules[tostring(baseClassId)];
    if rule and rule.onDie then
        rule.onDie(monster);
    end
end

-- 怪物变身时回调
function MonsterBuffEffectM:onMonsterTransform(monster)
    local baseClassId = monster:query("base_class_id");

    local rule = self.rules[tostring(baseClassId)];
    if rule and rule.onTransform then
        rule.onTransform(monster);
    end
end

-- 怪物受到伤害时回调
function MonsterBuffEffectM:onMonsterDamaged(monster)
    local baseClassId = monster:query("base_class_id");

    local rule = self.rules[tostring(baseClassId)];
    if rule and rule.onDamaged then
        rule.onDamaged(monster);
    end
end

-- 钢铁之门被摧毁的回调
function MonsterBuffEffectM:onSteelDoorDestory()
    local rule = self.rules["steel_door"];
    if rule and rule.onDestory then
        rule.onDestory();
    end
end

-- Buff系统注册的消息模块名
function MonsterBuffEffectM:addEventModule(name)
    if self.eventModuleNames == nil then
        self.eventModuleNames = {};
    end

    table.insert(self.eventModuleNames, name);
end


